package com.yanyu.space.common.auth.support;

import cn.dev33.satoken.stp.StpUtil;
import com.yanyu.space.common.auth.bean.enums.AuthType;
import com.yanyu.space.common.auth.support.StpUserUtil;
import com.yanyu.space.common.core.bean.auth.SpaceUser;
import lombok.extern.slf4j.Slf4j;

/**
 * @author yanyu
 */
@Slf4j
public class AuthUtil {

    public static final String USER = "space_user";

    public static void login(SpaceUser spaceUser, AuthType type) {
        if (type.equals(AuthType.ADMIN_TYPE)) {
            StpUtil.login(spaceUser.getId());
            StpUtil.getTokenInfo();
            StpUtil.getSession().set(USER, spaceUser);
        } else {
            StpUserUtil.login(spaceUser.getId());
            StpUserUtil.getTokenInfo();
            StpUserUtil.getSession().set(USER, spaceUser);
        }
    }

    public static void logout(AuthType type) {
        if (type.equals(AuthType.ADMIN_TYPE)) {
            StpUtil.logout();
        } else {
            StpUserUtil.logout();
        }
    }

    public static void kickOut(Long userId, AuthType type) {
        if (type.equals(AuthType.ADMIN_TYPE)) {
            StpUtil.logout(userId);
        } else {
            StpUserUtil.logout(userId);
        }
    }

    public static void resetSession(SpaceUser spaceUser, AuthType type) {
        if (type.equals(AuthType.ADMIN_TYPE)) {
            StpUtil.getSession().set(USER, spaceUser);
        } else {
            StpUserUtil.getSession().set(USER, spaceUser);
        }
    }

    public static SpaceUser getSession(AuthType type) {
        if (type.equals(AuthType.ADMIN_TYPE)) {
            return (SpaceUser) StpUtil.getSession().get(USER);
        } else {
            return (SpaceUser) StpUserUtil.getSession().get(USER);
        }
    }
}
